package com.vs.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.vs.common.SessionKeys;
import com.vs.dao.ExpensesOperation;
import com.vs.model.CustomMessage;
import com.vs.model.ExpenseForm;
import com.vs.model.ExpenseSearchForm;
import com.vs.model.ProjectForm;
import com.vs.model.User;
import com.vs.util.Constants;

public class ExpenseAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String next = "";
		System.out.println("ExpenseAction execute called");

		ExpenseForm expenseForm = (ExpenseForm) form;
		Constants.log.debug(expenseForm);

		ArrayList<CustomMessage> customErrors = new ArrayList<CustomMessage>();
		List<ExpenseForm> expenseList = null;

		String action = request.getParameter("action");
		Constants.log.debug("action1=" + action);
		System.out.println("action1=" + action);

		if (action == null) {
			action = expenseForm.getAction();
			System.out.println("action2=" + action);
		}
		
		HttpSession session = getActiveSession(request);
		User user = (User) session.getAttribute(SessionKeys.CURRENT_USER);
		if(expenseForm!=null)
		expenseForm.setAddedBy(user.getUserName());

		if (action != null) {
			if (action.equals("selectAll")) {
				next = "selectAll";
				Integer projectId = null;
				
				if(session.getAttribute("projectId")!=null)
				{
					projectId = Integer.parseInt(session.getAttribute(
					"projectId").toString());
				}
				else
				{
					projectId = Integer.parseInt(request.getAttribute(
					"projectId").toString());
				}
				expenseForm.setExpenseProjectId(projectId);
				
				session.removeAttribute("projectName");
				session.removeAttribute("projectId");
				session.setAttribute("projectId",
						expenseForm.getExpenseProjectId());
				session.setAttribute(
						"projectName",
						lookUpProjectName(session,
								expenseForm.getExpenseProjectId()));

			} else if (action.equals("add")) {
				boolean result = new ExpensesOperation()
						.addExpense(expenseForm);
				if (result) {
					next = "selectAll";
					Integer projectId = Integer.parseInt(getActiveSession(
							request).getAttribute("projectId").toString());
					expenseForm.setExpenseProjectId(projectId);
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.INFO);
					info.setMessageText("Expense "
							+ expenseForm.getExpenseTypeName()
							+ " is added successfully");
					customErrors.add(info);

				} else {
					next = "addError";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("Error while add expense details");
					customErrors.add(info);
				}
			} else if (action.equals("beforeEdit")) {
				Integer projectId = Integer.parseInt(getActiveSession(request)
						.getAttribute("projectId").toString());
				ExpenseForm expenseObj = new ExpensesOperation()
						.getExpense(expenseForm.getExpenseId());
				if (expenseObj != null) {
					next = "beforeEdit";

					expenseForm.setExpenseProjectId(projectId);
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.INFO);
					info.setMessageText("Expense "
							+ expenseForm.getExpenseTypeName()
							+ " is added successfully");
					customErrors.add(info);

					request.setAttribute("expenseObj", expenseObj);

				} else {
					next = "addError";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("Error while add expense details");
					customErrors.add(info);
				}
			} else if (action.equals("edit")) {
				boolean result = new ExpensesOperation()
						.editExpense(expenseForm);
				if (result) {
					next = "selectAll";
					Integer projectId = Integer.parseInt(getActiveSession(
							request).getAttribute("projectId").toString());
					expenseForm.setExpenseProjectId(projectId);
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.INFO);
					info.setMessageText("Expense "
							+ expenseForm.getExpenseTypeName()
							+ " is added successfully");
					customErrors.add(info);

				} else {
					next = "addError";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("Error while add expense details");
					customErrors.add(info);
				}
			} else if (action.equals("delete")) {

				Integer expenseId = Integer.parseInt(request
						.getParameter("expenseId"));
				boolean result = new ExpensesOperation()
						.deleteExpense(expenseId);
				Integer projectId = Integer.parseInt(getActiveSession(request)
						.getAttribute("projectId").toString());
				expenseForm.setExpenseProjectId(projectId);
				if (result) {
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.INFO);
					info.setMessageText("expenseId " + expenseId
							+ " is deleted successfully");
					customErrors.add(info);
					next = "selectAll";
				} else {
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("Error while deleting expense "
							+ expenseId);
					customErrors.add(info);
					next = "selectAll";
				}
				// ///expenseForm.setSearchType("search");

			}
			if (action.equals("search")) {
				next="selectAll";
			}

			if (next.equals("selectAll")) {
				ExpenseSearchForm searchForm = new ExpenseSearchForm();
				searchForm.setExpenseProjectId(expenseForm.getExpenseProjectId());
				expenseList = new ExpensesOperation()
						.getExpensesList(searchForm);
				
				if(expenseList==null || expenseList.size()==0)
				{
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("At present, no expenses are added to this project");
					customErrors.add(info);
				}
				
				request.setAttribute("reportName",
						expenseForm.getExpenseProjectName());
				request.setAttribute("expensesList", expenseList);
				
				getActiveSession(request).removeAttribute("expensesList");
				getActiveSession(request).setAttribute("expensesList", expenseList);
			}
		}
		System.out.println("customErrors size="+customErrors.size());
		request.setAttribute("customErrors", customErrors);
		System.out.println("next=" + next);
		return mapping.findForward(next);

	}

	@SuppressWarnings("unchecked")
	private String lookUpProjectName(HttpSession session, Integer projectId) {
		List<ProjectForm> plist = (List<ProjectForm>) session
				.getAttribute(SessionKeys.PROJECT_LOV);
		for (int i = 0; i < plist.size(); i++) {
			if (plist.get(i).getProjectId().equals(projectId))
				return plist.get(i).getProjectName();
		}
		return null;
	}

	private HttpSession getActiveSession(HttpServletRequest request) {
		return request.getSession(false);
	}

}
